#include "PaletteShower.h"

using namespace std;

PaletteShower::PaletteShower(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(344, 242);
    horizontalLayout = new QHBoxLayout(this);
    graphicsView1 = new QGraphicsView(this);
    graphicsView2 = new QGraphicsView(this);
    scene = new PaletteScene;
    scene2 = new PaletteScene;

    horizontalLayout->addWidget(graphicsView1);
    horizontalLayout->addWidget(graphicsView2);

    graphicsView1->setMouseTracking(true);
    graphicsView2->setMouseTracking(true);
    graphicsView1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    graphicsView1->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    graphicsView2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    graphicsView2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    graphicsView1->setStyleSheet("padding: 0px; border: 0px;");
    graphicsView2->setStyleSheet("padding: 0px; border: 0px;");

    scene->SetPalette(this);
    scene2->SetPalette(this);
}

PaletteShower::~PaletteShower()
{
    delete horizontalLayout;
    delete graphicsView1;
    delete graphicsView2;
    delete scene;
    delete scene2;
}

void PaletteShower::UpdateImage()
{

    QImage img = QImage(160, 224, QImage::Format_RGB888);
    QImage img2 = QImage(160, 224, QImage::Format_RGB888);

    QPainter painter(&img);
    QPainter painter2(&img2);

    // 反走样
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter2.setRenderHint(QPainter::Antialiasing, true);
    // 设置画笔颜色、宽度
    painter.setPen(QPen(QColor(0, 0, 0), 1));
    painter2.setPen(QPen(QColor(0, 0, 0), 1));
    // 设置画刷颜色

    for (uint8_t i = 0; i < 32; i++) {
        for (uint8_t j = 0; j < 8; j++) {

            auto r = palette->GetColor(i + (j * 32)).r;
            auto g = palette->GetColor(i + (j * 32)).g;
            auto b = palette->GetColor(i + (j * 32)).b;
            auto a = palette->GetColor(i + (j * 32)).a;
            painter.setBrush(QColor(r, g, b));
            painter.drawRect(j * 20, i * 7, 20, 7);
            painter2.setBrush(QColor(a, a, a));
            painter2.drawRect(j * 20, i * 7, 20, 7);
        }
    }

    painter.setPen(QPen(QColor(255, 255, 0), 2));
    painter2.setPen(QPen(QColor(255, 255, 0), 2));
    auto passedColor = palette->GetColor(passedColorIndex);
    painter.setBrush(QColor(passedColor.r, passedColor.g, passedColor.b));
    painter.drawRect(passedColorIndex / 32 * 20, passedColorIndex % 32 * 7, 20, 7);
    painter2.setBrush(QColor(passedColor.a, passedColor.a, passedColor.a));
    painter2.drawRect(passedColorIndex / 32 * 20, passedColorIndex % 32 * 7, 20, 7);

    painter.setPen(QPen(QColor(255, 0, 0), 2));
    painter2.setPen(QPen(QColor(255, 0, 0), 2));
    auto pickedColor = palette->GetColor(pickedColorIndex);
    painter.setBrush(QColor(pickedColor.r, pickedColor.g, pickedColor.b));
    painter.drawRect(pickedColorIndex / 32 * 20, pickedColorIndex % 32 * 7, 20, 7);
    painter2.setBrush(QColor(pickedColor.a, pickedColor.a, pickedColor.a));
    painter2.drawRect(pickedColorIndex / 32 * 20, pickedColorIndex % 32 * 7, 20, 7);

    graphicsView1->resize(img.width(), img.height());
    graphicsView2->resize(img2.width(), img2.height());

    scene->addPixmap(QPixmap::fromImage(img));
    scene2->addPixmap(QPixmap::fromImage(img2));

    graphicsView1->setScene(scene);
    graphicsView1->adjustSize();
    graphicsView1->show();

    graphicsView2->setScene(scene2);
    graphicsView2->adjustSize();
    graphicsView2->show();
}

void PaletteShower::SetPalette(const shared_ptr<IPalette> &palette_)
{
    this->palette = palette_;
    UpdateImage();
}
